package slick.path2glory.tutorial1;

import java.awt.Font;
import java.util.ArrayList;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class CharacterSelectState extends BasicGameState {

	int stateID = 0;

	Image background = null;
	Image awesomeFaceImg = null;
	Image demonAwesomeFaceImg = null;
	Image one = null;
	Image two = null;
	Image menuOptions = null;
	Image startGameOption = null;
	Image exitOption = null;
	Input input;

	private enum STATES {
		PLAYER_NUM_STATE, ONE_PLAYER_SELECT_STATE, TWO_PLAYER_SELECT_STATE
	}

	private STATES currentState = null;

	public CharacterSelectState(int stateID) {
		this.stateID = stateID;
	}

	@Override
	public int getID() {
		return stateID;
	}

	public void enter(GameContainer gc, StateBasedGame sb)
			throws SlickException {
		super.enter(gc, sb);

		currentState = STATES.PLAYER_NUM_STATE;

	}

	public void init(GameContainer arg0, StateBasedGame arg1)
			throws SlickException {

		background = new Image("data/menu.jpg");
		awesomeFaceImg = new Image("data/AF_N/AF_N1.png");
		demonAwesomeFaceImg = new Image("data/DAF/DAF1.png");
		one = new Image("data/one.png");
		two = new Image("data/two.png");
		menuOptions = new Image("data/menuoptions.png");
		startGameOption = menuOptions.getSubImage(0, 0, 377, 71);
		exitOption = menuOptions.getSubImage(0, 71, 377, 71);

	}

	public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2)
			throws SlickException {

		background.draw(0, 0);

		switch (currentState) {
		case PLAYER_NUM_STATE:
			one.draw(600, 400);
			two.draw(700, 400);
			break;
		case ONE_PLAYER_SELECT_STATE:

			onePSelectRender();
			break;
		case TWO_PLAYER_SELECT_STATE:

			twoPSelectRender();
			break;
		}

	}

	public void update(GameContainer gc, StateBasedGame sb, int delta)
			throws SlickException {

		input = gc.getInput();

		int mouseX = input.getMouseX();
		int mouseY = input.getMouseY();

		boolean insideAF = false;
		boolean insideDAF = false;
		boolean insideAF2 = false;
		boolean insideDAF2 = false;
		boolean inside1 = false;
		boolean inside2 = false;
		boolean insideStart = false;
		boolean insideExit = false;

		switch (currentState) {
		case PLAYER_NUM_STATE:
			if (mouseX >= 600 && mouseX <= 664 && mouseY >= 400
					&& mouseY <= 464) {
				inside1 = true;
			}
			if (mouseX >= 700 && mouseX <= 764 && mouseY >= 400
					&& mouseY <= 464) {
				inside2 = true;
			}

			if (inside1) {
				if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {

					SlickBasicGame.players = 1;
					currentState = STATES.ONE_PLAYER_SELECT_STATE;
				}
			}

			if (inside2) {
				if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
					SlickBasicGame.players = 2;
					currentState = STATES.TWO_PLAYER_SELECT_STATE;
				}
			}
			break;
		case ONE_PLAYER_SELECT_STATE:
			if (onePInsideAF(mouseX, mouseY)) {
				insideAF = true;
			} else if (onePInsideDAF(mouseX, mouseY)) {
				insideDAF = true;
			}
			if (mouseX >= 300 && mouseX <= 677 && mouseY >= 600
					&& mouseY <= 671) {
				insideStart = true;
			}
			if (mouseX >= 600 && mouseX <= 977 && mouseY >= 600
					&& mouseY <= 671) {
				insideExit = true;
			}

			onePInsideResult(insideAF, insideDAF, insideStart, insideExit, sb);

			break;
		case TWO_PLAYER_SELECT_STATE:
			if (twoPInsideAF(mouseX, mouseY)) {
				insideAF = true;
			} else if (twoPInsideDAF(mouseX, mouseY)) {
				insideDAF = true;
			}
			if (twoPInsideAF2(mouseX, mouseY)) {
				insideAF2 = true;
			} else if (twoPInsideDAF2(mouseX, mouseY)) {
				insideDAF2 = true;
			}

			if (mouseX >= 300 && mouseX <= 677 && mouseY >= 600
					&& mouseY <= 671) {
				insideStart = true;
			}
			if (mouseX >= 600 && mouseX <= 977 && mouseY >= 600
					&& mouseY <= 671) {
				insideExit = true;
			}

			twoPInsideResult(insideAF, insideDAF, insideStart, insideExit, insideAF2, insideDAF2, sb);
			break;
		}

	}

	public void onePSelectRender() {
		awesomeFaceImg.draw(
				SlickBasicGame.SCREEN_WIDTH / 2
						- (awesomeFaceImg.getWidth() / 2),
				SlickBasicGame.SCREEN_HEIGHT / 2
						- (awesomeFaceImg.getHeight() / 2) - 100, 1);
		demonAwesomeFaceImg.draw(
				SlickBasicGame.SCREEN_WIDTH / 2
						- (demonAwesomeFaceImg.getWidth() / 2) + 100,
				SlickBasicGame.SCREEN_HEIGHT / 2
						- (demonAwesomeFaceImg.getHeight() / 2) - 100, 1);
		startGameOption.draw(300, 600);
		exitOption.draw(600, 600);
	}

	public void twoPSelectRender() {
		one.draw(SlickBasicGame.SCREEN_WIDTH / 2
				- (awesomeFaceImg.getWidth() / 2) - 100,
				SlickBasicGame.SCREEN_HEIGHT / 2
						- (awesomeFaceImg.getHeight() / 2) - 100);
		two.draw(SlickBasicGame.SCREEN_WIDTH / 2
				- (awesomeFaceImg.getWidth() / 2) - 100,
				SlickBasicGame.SCREEN_HEIGHT / 2
						- (awesomeFaceImg.getHeight() / 2) + 100);
		awesomeFaceImg.draw(
				SlickBasicGame.SCREEN_WIDTH / 2
						- (awesomeFaceImg.getWidth() / 2),
				SlickBasicGame.SCREEN_HEIGHT / 2
						- (awesomeFaceImg.getHeight() / 2) - 100, 1);
		demonAwesomeFaceImg.draw(
				SlickBasicGame.SCREEN_WIDTH / 2
						- (demonAwesomeFaceImg.getWidth() / 2) + 100,
				SlickBasicGame.SCREEN_HEIGHT / 2
						- (demonAwesomeFaceImg.getHeight() / 2) - 100, 1);
		awesomeFaceImg.draw(
				SlickBasicGame.SCREEN_WIDTH / 2
						- (awesomeFaceImg.getWidth() / 2),
				SlickBasicGame.SCREEN_HEIGHT / 2
						- (awesomeFaceImg.getHeight() / 2) + 100, 1);
		demonAwesomeFaceImg.draw(
				SlickBasicGame.SCREEN_WIDTH / 2
						- (demonAwesomeFaceImg.getWidth() / 2) + 100,
				SlickBasicGame.SCREEN_HEIGHT / 2
						- (demonAwesomeFaceImg.getHeight() / 2) + 100, 1);
		startGameOption.draw(300, 600);
		exitOption.draw(600, 600);
	}
	
	public boolean onePInsideAF(int mouseX, int mouseY){
		if ((mouseX >= SlickBasicGame.SCREEN_WIDTH / 2
				- (awesomeFaceImg.getWidth() / 2) && mouseX <= SlickBasicGame.SCREEN_WIDTH
				/ 2 + awesomeFaceImg.getWidth() / 2)
				&& (mouseY >= SlickBasicGame.SCREEN_HEIGHT / 2
						- (awesomeFaceImg.getHeight() / 2) - 100 && mouseY <= SlickBasicGame.SCREEN_HEIGHT
						/ 2 + awesomeFaceImg.getHeight() / 2 - 100)) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean twoPInsideAF(int mouseX, int mouseY){
		if ((mouseX >= SlickBasicGame.SCREEN_WIDTH / 2
				- (awesomeFaceImg.getWidth() / 2) && mouseX <= SlickBasicGame.SCREEN_WIDTH
				/ 2 + awesomeFaceImg.getWidth() / 2)
				&& (mouseY >= SlickBasicGame.SCREEN_HEIGHT / 2
						- (awesomeFaceImg.getHeight() / 2) - 100 && mouseY <= SlickBasicGame.SCREEN_HEIGHT
						/ 2 + awesomeFaceImg.getHeight() / 2 - 100)) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean onePInsideDAF(int mouseX, int mouseY){
		if ((mouseX >= SlickBasicGame.SCREEN_WIDTH / 2
				- (demonAwesomeFaceImg.getWidth() / 2) + 100 && mouseX <= SlickBasicGame.SCREEN_WIDTH
				/ 2 + demonAwesomeFaceImg.getWidth() / 2 + 100)
				&& (mouseY >= SlickBasicGame.SCREEN_HEIGHT / 2 + 80
						- (demonAwesomeFaceImg.getHeight() / 2) - 150 && mouseY <= SlickBasicGame.SCREEN_HEIGHT
						/ 2
						+ 80
						+ demonAwesomeFaceImg.getHeight()
						/ 2
						- 150)) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean twoPInsideDAF(int mouseX, int mouseY){
		if ((mouseX >= SlickBasicGame.SCREEN_WIDTH / 2
				- (demonAwesomeFaceImg.getWidth() / 2) + 100 && mouseX <= SlickBasicGame.SCREEN_WIDTH
				/ 2 + demonAwesomeFaceImg.getWidth() / 2 + 100)
				&& (mouseY >= SlickBasicGame.SCREEN_HEIGHT / 2 + 80
						- (demonAwesomeFaceImg.getHeight() / 2) - 150 && mouseY <= SlickBasicGame.SCREEN_HEIGHT
						/ 2
						+ 80
						+ demonAwesomeFaceImg.getHeight()
						/ 2
						- 150)) {
			return  true;
		} else {
			return false;
		}
	}
	
	public boolean twoPInsideAF2(int mouseX, int mouseY){
		if ((mouseX >= SlickBasicGame.SCREEN_WIDTH / 2
				- (awesomeFaceImg.getWidth() / 2) && mouseX <= SlickBasicGame.SCREEN_WIDTH
				/ 2 + awesomeFaceImg.getWidth() / 2)
				&& (mouseY >= SlickBasicGame.SCREEN_HEIGHT / 2
						- (awesomeFaceImg.getHeight() / 2) + 100 && mouseY <= SlickBasicGame.SCREEN_HEIGHT
						/ 2 + awesomeFaceImg.getHeight() / 2 + 100)) {
			return true;
		} else {
			return false;
		}
	}
	
	public boolean twoPInsideDAF2(int mouseX, int mouseY){
		if ((mouseX >= SlickBasicGame.SCREEN_WIDTH / 2
				- (demonAwesomeFaceImg.getWidth() / 2) + 100 && mouseX <= SlickBasicGame.SCREEN_WIDTH
				/ 2 + demonAwesomeFaceImg.getWidth() / 2 + 100)
				&& (mouseY >= SlickBasicGame.SCREEN_HEIGHT / 2 + 80
						- (demonAwesomeFaceImg.getHeight() / 2) + 30 && mouseY <= SlickBasicGame.SCREEN_HEIGHT
						/ 2 + 80 + demonAwesomeFaceImg.getHeight() / 2 + 30)) {
			return true;
		} else {
			return false;
		}
	}
	
	public void onePInsideResult(boolean insideAF, boolean insideDAF, boolean insideStart, boolean insideExit, StateBasedGame sb){
		if (insideAF) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {

				SlickBasicGame.player1character = 1;
			}
		}

		if (insideDAF) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				SlickBasicGame.player1character = 2;
			}
		}
		if (insideStart) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {

				sb.enterState(SlickBasicGame.GAMEPLAYSTATE);
			}
		}
		if (insideExit) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {

				sb.enterState(SlickBasicGame.MAINMENUSTATE);
			}
		}
	}
	
	public void twoPInsideResult(boolean insideAF, boolean insideDAF, boolean insideStart, boolean insideExit, boolean insideAF2, boolean insideDAF2, StateBasedGame sb){
		if (insideAF) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {

				SlickBasicGame.player1character = 1;
			}
		}

		if (insideDAF) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				SlickBasicGame.player1character = 2;
			}
		}

		if (insideAF2) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {

				SlickBasicGame.player2character = 1;
			}
		}

		if (insideDAF2) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				SlickBasicGame.player2character = 2;
			}
		}

		if (insideStart) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {

				sb.enterState(SlickBasicGame.GAMEPLAYSTATE);
			}
		}
		if (insideExit) {
			if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {

				sb.enterState(SlickBasicGame.MAINMENUSTATE);
			}
		}
	}

}